//
//  WaterFCell.m
//  CollectionView
//
//  Created by d2space on 14-2-26.
//  Copyright (c) 2014年 D2space. All rights reserved.
//

#import "WaterFCell.h"
#import "XDHeader.h"
#import "UIImageView+WebCache.h"
#define CELLWIDTH   140
@interface WaterFCell ()
{
    WaterFlow *_object;
}
@end

@implementation WaterFCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    _object = nil;
    
    [_imageView setImage:nil];
    [_imageView setFrame:CGRectZero];
    [_titleLabel setFrame:CGRectZero];
    [_nameLabel setFrame:CGRectZero];
//    [_eyeImageView setImage:nil];
//    [_eyeImageView setFrame:CGRectZero];
    
    
    [_countLabel setFrame:CGRectZero];
    [_backView setFrame:CGRectZero];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    float radio = 0;
    if ([_object.ratio floatValue] == 0) {
        radio = 0.889;
    }else{
        radio = [_object.ratio floatValue];
    }
    
    float width = CELLWIDTH;
    
    float height = width  / radio;
    
    float titleHeight = [_object.artTitle sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(CELLWIDTH, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height;
    
    float nameHeight = 15;
    
    _imageView.frame = CGRectMake(10, 0, 140, height);
    [_imageView setImageWithURL:[NSURL URLWithString:_object.urlString] placeholderImage:[UIImage imageNamed:@"placehold_image"]];
    
    _backView.frame = CGRectMake(CGRectGetMinX(_imageView.frame), CGRectGetMaxY(_imageView.frame), CELLWIDTH, 55);
    _titleLabel.text = _object.artTitle;
//    DDLOG(@"length = %d",_object.artTitle.length);

    if (_object.artTitle.length>18) {// m by qin
        titleHeight -=10;
        nameHeight -=10;
    }
    _titleLabel.frame = CGRectMake(CGRectGetMinX(_imageView.frame)+10, CGRectGetMaxY(_imageView.frame), CELLWIDTH-10, titleHeight + 10);
    
    _nameLabel.text = _object.artistName;
    _nameLabel.frame = CGRectMake(CGRectGetMinX(_titleLabel.frame), CGRectGetMaxY(_titleLabel.frame), 75, nameHeight + 10);
    
    _eyeImageView.frame = CGRectMake(_nameLabel.frame.origin.x+_nameLabel.frame.size.width+5, _nameLabel.frame.origin.y+8, 12, 8);
    
    _countLabel.text = [NSString stringWithFormat:@"%@",_object.artCount];
    _countLabel.frame = CGRectMake(_eyeImageView.frame.origin.x+_eyeImageView.frame.size.width+5, _nameLabel.frame.origin.y, 25, _nameLabel.frame.size.height);
    
}

- (void)setWaterObject:(WaterFlow *)object
{
    _object = object;
    
    DDLOG(@"%@",_object.ratio);
    
    [self layoutSubviews];
}

#pragma mark - Setup
- (void)setup
{
    [self setupTextView];
}

- (void)setupTextView
{
    _backView = [[UIView alloc] initWithFrame:CGRectZero];
    _backView.backgroundColor = [UIColor whiteColor];
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont systemFontOfSize:14.0];
    
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _nameLabel.backgroundColor = [UIColor clearColor];
    _nameLabel.font = [UIFont systemFontOfSize:13.0];
    
    _eyeImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    _eyeImageView.image = [UIImage imageNamed:@"water_eye"];
    
    
    _countLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _countLabel.font = [UIFont systemFontOfSize:13.0];
    
    
    [self.contentView addSubview:_imageView];
    [self.contentView addSubview:_backView];
    [self.contentView addSubview:_titleLabel];
    [self.contentView addSubview:_nameLabel];
    
    [self.contentView addSubview:_eyeImageView];
    [self.contentView addSubview:_countLabel];
}

+ (CGSize)waterCellSizeWithObject:(WaterFlow *)object;
{    
    float radio = 0;
    if ([object.ratio floatValue] == 0) {
        radio = 0.889;
    }else{
        radio = [object.ratio floatValue];
    }
    
    float width = CELLWIDTH;
    
    float height = width  / radio;
//    
//    float titleHeight = [object.artTitle sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(135, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height;
//    
//    float nameHeight = 15;
    
    return CGSizeMake(160,height + 55);
}

@end
